<!DOCTYPE html>




<html class="theme-next gemini" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="MongoDB,Map reduce," />





  <link rel="alternate" href="/atom.xml" title="流水不腐小夏" type="application/atom+xml" />






<meta name="description" content="MongoDB查询总结   MongoDB查询总结 介绍 普通查询 查询举例   聚合查询 Map-Reduce 接口方法定义 参数说明   示例说明 总结 相关资料     介绍前面写过一篇关于Mongodb的例子——浅谈MongoDB数据库，当时使用的只是简单的查询，然后后面业务变的有点复杂，原先没有仔细研究过Mongodb的查询，以为就是简单调用下find就可以了，乃衣服。 所以今天特地举">
<meta name="keywords" content="MongoDB,Map reduce">
<meta property="og:type" content="article">
<meta property="og:title" content="MongoDB查询总结">
<meta property="og:url" content="http://mycommons.cn/2017/10/24/MongoDB-Query/index.html">
<meta property="og:site_name" content="流水不腐小夏">
<meta property="og:description" content="MongoDB查询总结   MongoDB查询总结 介绍 普通查询 查询举例   聚合查询 Map-Reduce 接口方法定义 参数说明   示例说明 总结 相关资料     介绍前面写过一篇关于Mongodb的例子——浅谈MongoDB数据库，当时使用的只是简单的查询，然后后面业务变的有点复杂，原先没有仔细研究过Mongodb的查询，以为就是简单调用下find就可以了，乃衣服。 所以今天特地举">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://docs.mongodb.com/manual/_images/map-reduce.bakedsvg.svg">
<meta property="og:updated_time" content="2017-10-25T11:47:47.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="MongoDB查询总结">
<meta name="twitter:description" content="MongoDB查询总结   MongoDB查询总结 介绍 普通查询 查询举例   聚合查询 Map-Reduce 接口方法定义 参数说明   示例说明 总结 相关资料     介绍前面写过一篇关于Mongodb的例子——浅谈MongoDB数据库，当时使用的只是简单的查询，然后后面业务变的有点复杂，原先没有仔细研究过Mongodb的查询，以为就是简单调用下find就可以了，乃衣服。 所以今天特地举">
<meta name="twitter:image" content="https://docs.mongodb.com/manual/_images/map-reduce.bakedsvg.svg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"always","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: 'GRLHBBCD9E',
      apiKey: '759ab3f5c09b132b3ed4842a44c53c45',
      indexName: 'mycommons',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://mycommons.cn/2017/10/24/MongoDB-Query/"/>





  <title>MongoDB查询总结 | 流水不腐小夏</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?55458ffca1f60dd283f11b9a7f82b262";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">流水不腐小夏</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">http://mycommons.cn</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  
  <div class="algolia-popup popup search-popup">
    <div class="algolia-search">
      <div class="algolia-search-input-icon">
        <i class="fa fa-search"></i>
      </div>
      <div class="algolia-search-input" id="algolia-search-input"></div>
    </div>

    <div class="algolia-results">
      <div id="algolia-stats"></div>
      <div id="algolia-hits"></div>
      <div id="algolia-pagination" class="algolia-pagination"></div>
    </div>

    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
  </div>




    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://mycommons.cn/2017/10/24/MongoDB-Query/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="流水不腐小夏">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="流水不腐小夏">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">MongoDB查询总结</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-24T18:22:03+08:00">
                2017-10-24
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/数据库/" itemprop="url" rel="index">
                    <span itemprop="name">数据库</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/10/24/MongoDB-Query/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2017/10/24/MongoDB-Query/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv">本文总阅读量
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>次
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="MongoDB查询总结"><a href="#MongoDB查询总结" class="headerlink" title="MongoDB查询总结"></a>MongoDB查询总结</h1><hr>
<!-- TOC -->
<ul>
<li><a href="#mongodb查询总结">MongoDB查询总结</a><ul>
<li><a href="#介绍">介绍</a></li>
<li><a href="#普通查询">普通查询</a><ul>
<li><a href="#查询举例">查询举例</a></li>
</ul>
</li>
<li><a href="#聚合查询">聚合查询</a></li>
<li><a href="#map-reduce">Map-Reduce</a><ul>
<li><a href="#接口方法定义">接口方法定义</a></li>
<li><a href="#参数说明">参数说明</a></li>
</ul>
</li>
<li><a href="#示例说明">示例说明</a></li>
<li><a href="#总结">总结</a></li>
<li><a href="#相关资料">相关资料</a></li>
</ul>
</li>
</ul>
<!-- /TOC -->
<h2 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h2><p>前面写过一篇关于Mongodb的例子——<a href="http://www.jianshu.com/p/9e1d34eb1d4c" target="_blank" rel="external">浅谈MongoDB数据库</a>，当时使用的只是简单的查询，然后后面业务变的有点复杂，原先没有仔细研究过Mongodb的查询，以为就是简单调用下<code>find</code>就可以了，乃衣服。</p>
<p>所以今天特地举例说明一下Mongo中查询问题。</p>
<p>Mongo查询可以分为2种：</p>
<ul>
<li><p>普通查询，类似于Sql中的 <code>select where</code></p>
</li>
<li><p>聚合查询，类似于Sql中的 <code>group by</code></p>
</li>
</ul>
<h2 id="普通查询"><a href="#普通查询" class="headerlink" title="普通查询"></a>普通查询</h2><p>首先放一下<a href="https://docs.mongodb.com/manual/tutorial/query-documents/" target="_blank" rel="external">官方文档</a>，普通查询主要用到<code>db.collection.find()</code>函数。</p>
<p>定义下示例数据库，下面是是初始化数据，可以在Mongo中的控制台执行。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">db.inventory.insertMany([</div><div class="line">   &#123; <span class="attr">item</span>: <span class="string">"journal"</span>, <span class="attr">qty</span>: <span class="number">25</span>, <span class="attr">size</span>: &#123; <span class="attr">h</span>: <span class="number">14</span>, <span class="attr">w</span>: <span class="number">21</span>, <span class="attr">uom</span>: <span class="string">"cm"</span> &#125;, <span class="attr">status</span>: <span class="string">"A"</span> &#125;,</div><div class="line">   &#123; <span class="attr">item</span>: <span class="string">"notebook"</span>, <span class="attr">qty</span>: <span class="number">50</span>, <span class="attr">size</span>: &#123; <span class="attr">h</span>: <span class="number">8.5</span>, <span class="attr">w</span>: <span class="number">11</span>, <span class="attr">uom</span>: <span class="string">"in"</span> &#125;, <span class="attr">status</span>: <span class="string">"A"</span> &#125;,</div><div class="line">   &#123; <span class="attr">item</span>: <span class="string">"paper"</span>, <span class="attr">qty</span>: <span class="number">100</span>, <span class="attr">size</span>: &#123; <span class="attr">h</span>: <span class="number">8.5</span>, <span class="attr">w</span>: <span class="number">11</span>, <span class="attr">uom</span>: <span class="string">"in"</span> &#125;, <span class="attr">status</span>: <span class="string">"D"</span> &#125;,</div><div class="line">   &#123; <span class="attr">item</span>: <span class="string">"planner"</span>, <span class="attr">qty</span>: <span class="number">75</span>, <span class="attr">size</span>: &#123; <span class="attr">h</span>: <span class="number">22.85</span>, <span class="attr">w</span>: <span class="number">30</span>, <span class="attr">uom</span>: <span class="string">"cm"</span> &#125;, <span class="attr">status</span>: <span class="string">"D"</span> &#125;,</div><div class="line">   &#123; <span class="attr">item</span>: <span class="string">"postcard"</span>, <span class="attr">qty</span>: <span class="number">45</span>, <span class="attr">size</span>: &#123; <span class="attr">h</span>: <span class="number">10</span>, <span class="attr">w</span>: <span class="number">15.25</span>, <span class="attr">uom</span>: <span class="string">"cm"</span> &#125;, <span class="attr">status</span>: <span class="string">"A"</span> &#125;</div><div class="line">]);</div></pre></td></tr></table></figure>
<ul>
<li>查询所有</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">db.inventory.find( &#123;&#125; )</div></pre></td></tr></table></figure>
<p>映射Sql语句</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> * <span class="keyword">FROM</span> inventory</div></pre></td></tr></table></figure>
<ul>
<li>条件查询</li>
</ul>
<p>语法格式</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&#123; &lt;field1&gt;: <span class="xml"><span class="tag">&lt;<span class="name">value1</span>&gt;</span>, ... &#125;</span></div></pre></td></tr></table></figure>
<p>比如查询<code>status</code>为<code>D</code>记录。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">db.inventory.find( &#123; <span class="attr">status</span>: <span class="string">"D"</span> &#125; )</div></pre></td></tr></table></figure>
<p>映射Sql语句</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> * <span class="keyword">FROM</span> inventory <span class="keyword">WHERE</span> <span class="keyword">status</span> = <span class="string">"D"</span></div></pre></td></tr></table></figure>
<ul>
<li>使用操作符进行条件查询</li>
</ul>
<p>语法格式</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&#123; &lt;field1&gt;: &#123; &lt;operator1&gt;: <span class="xml"><span class="tag">&lt;<span class="name">value1</span>&gt;</span> &#125;, ... &#125;</span></div></pre></td></tr></table></figure>
<p>比如查询满足<code>status</code>是数组<code>[A,D]</code>中的记录</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">db.inventory.find( &#123; <span class="attr">status</span>: &#123; <span class="attr">$in</span>: [ <span class="string">"A"</span>, <span class="string">"D"</span> ] &#125; &#125; )</div></pre></td></tr></table></figure>
<p>映射Sql语句</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> * <span class="keyword">FROM</span> inventory <span class="keyword">WHERE</span> <span class="keyword">status</span> <span class="keyword">in</span> (<span class="string">"A"</span>, <span class="string">"D"</span>)</div></pre></td></tr></table></figure>
<ul>
<li>AND 条件查询</li>
</ul>
<p>直接在find函数指定多个字段满足即可，这样就是 and 条件。</p>
<p>比如下面语句就是 <code>status</code> 为 <code>A</code>，<code>qty</code> 小于 <code>30</code>。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">db.inventory.find( &#123; <span class="attr">status</span>: <span class="string">"A"</span>, <span class="attr">qty</span>: &#123; <span class="attr">$lt</span>: <span class="number">30</span> &#125; &#125; )</div></pre></td></tr></table></figure>
<p>映射Sql语句</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> * <span class="keyword">FROM</span> inventory <span class="keyword">WHERE</span> <span class="keyword">status</span> = <span class="string">"A"</span> <span class="keyword">AND</span> qty &lt; <span class="number">30</span></div></pre></td></tr></table></figure>
<ul>
<li>OR 条件查询</li>
</ul>
<p>OR 和 AND 就不一样了，需要用到操作符 <code>$or</code>，如下所示。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">db.inventory.find( &#123; <span class="attr">$or</span>: [ &#123; <span class="attr">status</span>: <span class="string">"A"</span> &#125;, &#123; <span class="attr">qty</span>: &#123; <span class="attr">$lt</span>: <span class="number">30</span> &#125; &#125; ] &#125; )</div></pre></td></tr></table></figure>
<p>类似于SQL中的</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> * <span class="keyword">FROM</span> inventory <span class="keyword">WHERE</span> <span class="keyword">status</span> = <span class="string">"A"</span> <span class="keyword">OR</span> qty &lt; <span class="number">30</span></div></pre></td></tr></table></figure>
<ul>
<li>OR 和 AND 集合一起</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">db.inventory.find( &#123;</div><div class="line">     status: <span class="string">"A"</span>,</div><div class="line">     $or: [ &#123; <span class="attr">qty</span>: &#123; <span class="attr">$lt</span>: <span class="number">30</span> &#125; &#125;, &#123; <span class="attr">item</span>: <span class="regexp">/^p/</span> &#125; ]</div><div class="line">&#125; )</div></pre></td></tr></table></figure>
<p>表示这样的意思。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> * <span class="keyword">FROM</span> inventory <span class="keyword">WHERE</span> <span class="keyword">status</span> = <span class="string">"A"</span> <span class="keyword">AND</span> ( qty &lt; <span class="number">30</span> <span class="keyword">OR</span> item <span class="keyword">LIKE</span> <span class="string">"p%"</span>)</div></pre></td></tr></table></figure>
<h3 id="查询举例"><a href="#查询举例" class="headerlink" title="查询举例"></a>查询举例</h3><ul>
<li>查询全部</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> *</div><div class="line"><span class="keyword">FROM</span> people</div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">db.people.find()</div></pre></td></tr></table></figure>
<ul>
<li>指定字段</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> <span class="keyword">id</span>,</div><div class="line">       user_id,</div><div class="line">       <span class="keyword">status</span></div><div class="line"><span class="keyword">FROM</span> people</div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">db.people.find(</div><div class="line">    &#123; &#125;,</div><div class="line">    &#123; <span class="attr">user_id</span>: <span class="number">1</span>, <span class="attr">status</span>: <span class="number">1</span> &#125;</div><div class="line">)</div></pre></td></tr></table></figure>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> user_id, <span class="keyword">status</span></div><div class="line"><span class="keyword">FROM</span> people</div></pre></td></tr></table></figure>
<ul>
<li>指定字段，不显示<code>_id</code></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">db.people.find(</div><div class="line">    &#123; &#125;,</div><div class="line">    &#123; <span class="attr">user_id</span>: <span class="number">1</span>, <span class="attr">status</span>: <span class="number">1</span>, <span class="attr">_id</span>: <span class="number">0</span> &#125;</div><div class="line">)</div></pre></td></tr></table></figure>
<ul>
<li>条件查询全部</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> *</div><div class="line"><span class="keyword">FROM</span> people</div><div class="line"><span class="keyword">WHERE</span> <span class="keyword">status</span> = <span class="string">"A"</span></div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">db.people.find(</div><div class="line">    &#123; <span class="attr">status</span>: <span class="string">"A"</span> &#125;</div><div class="line">)</div></pre></td></tr></table></figure>
<ul>
<li>条件查询指定字段</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> user_id, <span class="keyword">status</span></div><div class="line"><span class="keyword">FROM</span> people</div><div class="line"><span class="keyword">WHERE</span> <span class="keyword">status</span> = <span class="string">"A"</span></div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">db.people.find(</div><div class="line">    &#123; <span class="attr">status</span>: <span class="string">"A"</span> &#125;,</div><div class="line">    &#123; <span class="attr">user_id</span>: <span class="number">1</span>, <span class="attr">status</span>: <span class="number">1</span>, <span class="attr">_id</span>: <span class="number">0</span> &#125;</div><div class="line">)</div></pre></td></tr></table></figure>
<ul>
<li>条件查询不等于</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> *</div><div class="line"><span class="keyword">FROM</span> people</div><div class="line"><span class="keyword">WHERE</span> <span class="keyword">status</span> != <span class="string">"A"</span></div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">db.people.find(</div><div class="line">    &#123; <span class="attr">status</span>: &#123; <span class="attr">$ne</span>: <span class="string">"A"</span> &#125; &#125;</div><div class="line">)</div></pre></td></tr></table></figure>
<ul>
<li>条件查询 AND</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> *</div><div class="line"><span class="keyword">FROM</span> people</div><div class="line"><span class="keyword">WHERE</span> <span class="keyword">status</span> = <span class="string">"A"</span></div><div class="line"><span class="keyword">AND</span> age = <span class="number">50</span></div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">db.people.find(</div><div class="line">    &#123; <span class="attr">status</span>: <span class="string">"A"</span>,</div><div class="line">      age: <span class="number">50</span> &#125;</div><div class="line">)</div></pre></td></tr></table></figure>
<ul>
<li>条件查询 OR</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> *</div><div class="line"><span class="keyword">FROM</span> people</div><div class="line"><span class="keyword">WHERE</span> <span class="keyword">status</span> = <span class="string">"A"</span></div><div class="line"><span class="keyword">OR</span> age = <span class="number">50</span></div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">db.people.find(</div><div class="line">    &#123; <span class="attr">$or</span>: [ &#123; <span class="attr">status</span>: <span class="string">"A"</span> &#125; ,</div><div class="line">             &#123; <span class="attr">age</span>: <span class="number">50</span> &#125; ] &#125;</div><div class="line">)</div></pre></td></tr></table></figure>
<ul>
<li>条件查询 &gt;</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> *</div><div class="line"><span class="keyword">FROM</span> people</div><div class="line"><span class="keyword">WHERE</span> age &gt; <span class="number">25</span></div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">db.people.find(</div><div class="line">    &#123; <span class="attr">age</span>: &#123; <span class="attr">$gt</span>: <span class="number">25</span> &#125; &#125;</div><div class="line">)</div></pre></td></tr></table></figure>
<ul>
<li>条件查询 &lt;</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> *</div><div class="line"><span class="keyword">FROM</span> people</div><div class="line"><span class="keyword">WHERE</span> age &lt; <span class="number">25</span></div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">db.people.find(</div><div class="line">   &#123; <span class="attr">age</span>: &#123; <span class="attr">$lt</span>: <span class="number">25</span> &#125; &#125;</div><div class="line">)</div></pre></td></tr></table></figure>
<ul>
<li>复杂的条件查询</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> *</div><div class="line"><span class="keyword">FROM</span> people</div><div class="line"><span class="keyword">WHERE</span> age &gt; <span class="number">25</span></div><div class="line"><span class="keyword">AND</span>   age &lt;= <span class="number">50</span></div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">db.people.find(</div><div class="line">   &#123; <span class="attr">age</span>: &#123; <span class="attr">$gt</span>: <span class="number">25</span>, <span class="attr">$lte</span>: <span class="number">50</span> &#125; &#125;</div><div class="line">)</div></pre></td></tr></table></figure>
<ul>
<li>条件查询 LIKE</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> *</div><div class="line"><span class="keyword">FROM</span> people</div><div class="line"><span class="keyword">WHERE</span> user_id <span class="keyword">like</span> <span class="string">"%bc%"</span></div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">db.people.find( &#123; <span class="attr">user_id</span>: <span class="regexp">/bc/</span> &#125; )</div><div class="line"></div><div class="line"><span class="comment">// OR</span></div><div class="line"></div><div class="line">db.people.find( &#123; <span class="attr">user_id</span>: &#123; <span class="attr">$regex</span>: <span class="regexp">/bc/</span> &#125; &#125; )</div></pre></td></tr></table></figure>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> *</div><div class="line"><span class="keyword">FROM</span> people</div><div class="line"><span class="keyword">WHERE</span> user_id <span class="keyword">like</span> <span class="string">"bc%"</span></div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">db.people.find( &#123; <span class="attr">user_id</span>: <span class="regexp">/^bc/</span> &#125; )</div><div class="line"></div><div class="line"><span class="comment">// OR</span></div><div class="line"></div><div class="line">db.people.find( &#123; <span class="attr">user_id</span>: &#123; <span class="attr">$regex</span>: <span class="regexp">/^bc/</span> &#125; &#125; )</div></pre></td></tr></table></figure>
<ul>
<li>排序</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> *</div><div class="line"><span class="keyword">FROM</span> people</div><div class="line"><span class="keyword">WHERE</span> <span class="keyword">status</span> = <span class="string">"A"</span></div><div class="line"><span class="keyword">ORDER</span> <span class="keyword">BY</span> user_id <span class="keyword">ASC</span></div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">db.people.find( &#123; <span class="attr">status</span>: <span class="string">"A"</span> &#125; ).sort( &#123; <span class="attr">user_id</span>: <span class="number">1</span> &#125; )</div></pre></td></tr></table></figure>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> *</div><div class="line"><span class="keyword">FROM</span> people</div><div class="line"><span class="keyword">WHERE</span> <span class="keyword">status</span> = <span class="string">"A"</span></div><div class="line"><span class="keyword">ORDER</span> <span class="keyword">BY</span> user_id <span class="keyword">DESC</span></div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">db.people.find( &#123; <span class="attr">status</span>: <span class="string">"A"</span> &#125; ).sort( &#123; <span class="attr">user_id</span>: <span class="number">-1</span> &#125; )</div></pre></td></tr></table></figure>
<ul>
<li>统计数量</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> <span class="keyword">COUNT</span>(*)</div><div class="line"><span class="keyword">FROM</span> people</div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">db.people.count()</div><div class="line"></div><div class="line"><span class="comment">// or</span></div><div class="line"></div><div class="line">db.people.find().count()</div></pre></td></tr></table></figure>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> <span class="keyword">COUNT</span>(user_id)</div><div class="line"><span class="keyword">FROM</span> people</div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">db.people.count( &#123; <span class="attr">user_id</span>: &#123; <span class="attr">$exists</span>: <span class="literal">true</span> &#125; &#125; )</div><div class="line">or</div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">db.people.find( &#123; <span class="attr">user_id</span>: &#123; <span class="attr">$exists</span>: <span class="literal">true</span> &#125; &#125; ).count()</div></pre></td></tr></table></figure>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> <span class="keyword">COUNT</span>(*)</div><div class="line"><span class="keyword">FROM</span> people</div><div class="line"><span class="keyword">WHERE</span> age &gt; <span class="number">30</span></div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">db.people.count( &#123; <span class="attr">age</span>: &#123; <span class="attr">$gt</span>: <span class="number">30</span> &#125; &#125; )</div><div class="line"></div><div class="line"><span class="comment">// or</span></div><div class="line"></div><div class="line">db.people.find( &#123; <span class="attr">age</span>: &#123; <span class="attr">$gt</span>: <span class="number">30</span> &#125; &#125; ).count()</div></pre></td></tr></table></figure>
<ul>
<li>去除重复distinct</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> <span class="keyword">DISTINCT</span>(<span class="keyword">status</span>)</div><div class="line"><span class="keyword">FROM</span> people</div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">db.people.distinct( <span class="string">"status"</span> )</div></pre></td></tr></table></figure>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> *</div><div class="line"><span class="keyword">FROM</span> people</div><div class="line"><span class="keyword">LIMIT</span> <span class="number">1</span></div></pre></td></tr></table></figure>
<ul>
<li>限制数量</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">db.people.findOne()</div><div class="line"></div><div class="line"><span class="comment">// or</span></div><div class="line"></div><div class="line">db.people.find().limit(<span class="number">1</span>)</div></pre></td></tr></table></figure>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> *</div><div class="line"><span class="keyword">FROM</span> people</div><div class="line"><span class="keyword">LIMIT</span> <span class="number">5</span></div><div class="line"><span class="keyword">SKIP</span> <span class="number">10</span></div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">db.people.find().limit(<span class="number">5</span>).skip(<span class="number">10</span>)</div></pre></td></tr></table></figure>
<ul>
<li>EXPLAIN</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">EXPLAIN</span> <span class="keyword">SELECT</span> *</div><div class="line"><span class="keyword">FROM</span> people</div><div class="line"><span class="keyword">WHERE</span> <span class="keyword">status</span> = <span class="string">"A"</span></div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">db.people.find( &#123; <span class="attr">status</span>: <span class="string">"A"</span> &#125; ).explain()</div></pre></td></tr></table></figure>
<h2 id="聚合查询"><a href="#聚合查询" class="headerlink" title="聚合查询"></a>聚合查询</h2><p>上面普通查询使用<code>find</code>函数即可，但是聚合查询使用另外一个函数<code>aggregate</code>，这里是<a href="https://docs.mongodb.com/manual/core/aggregation-pipeline/" target="_blank" rel="external">官方文档</a>。</p>
<p>初始化数据如下，有2个表 <code>orders</code> 和 <code>order_lineitem</code> ，外键关联<code>order_lineitem.order_id and the orders.id</code>。</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">&#123;</div><div class="line">  cust_id: "abc123",</div><div class="line">  ord_date: ISODate("2012-11-02T17:04:11.102Z"),</div><div class="line">  status: 'A',</div><div class="line">  price: 50,</div><div class="line">  items: [ &#123; sku: "xxx", qty: 25, price: 1 &#125;,</div><div class="line">           &#123; sku: "yyy", qty: 25, price: 1 &#125; ]</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>统计数量</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">db.orders.aggregate( [</div><div class="line">   &#123;</div><div class="line">     $group: &#123;</div><div class="line">        _id: <span class="literal">null</span>,</div><div class="line">        count: &#123; <span class="attr">$sum</span>: <span class="number">1</span> &#125;</div><div class="line">     &#125;</div><div class="line">   &#125;</div><div class="line">] )</div></pre></td></tr></table></figure>
<p>映射Sql语句</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> <span class="keyword">COUNT</span>(*) <span class="keyword">AS</span> <span class="keyword">count</span></div><div class="line"><span class="keyword">FROM</span> orders</div></pre></td></tr></table></figure>
<ul>
<li>计算总和</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">db.orders.aggregate( [</div><div class="line">   &#123;</div><div class="line">     $group: &#123;</div><div class="line">        _id: <span class="literal">null</span>,</div><div class="line">        total: &#123; <span class="attr">$sum</span>: <span class="string">"$price"</span> &#125;</div><div class="line">     &#125;</div><div class="line">   &#125;</div><div class="line">] )</div></pre></td></tr></table></figure>
<p>映射Sql语句</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> <span class="keyword">SUM</span>(price) <span class="keyword">AS</span> total</div><div class="line"><span class="keyword">FROM</span> orders</div></pre></td></tr></table></figure>
<ul>
<li>分组计算总和</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">db.orders.aggregate( [</div><div class="line">   &#123;</div><div class="line">     $group: &#123;</div><div class="line">        _id: <span class="string">"$cust_id"</span>,</div><div class="line">        total: &#123; <span class="attr">$sum</span>: <span class="string">"$price"</span> &#125;</div><div class="line">     &#125;</div><div class="line">   &#125;</div><div class="line">] )</div></pre></td></tr></table></figure>
<p>映射Sql语句</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> cust_id,</div><div class="line">       <span class="keyword">SUM</span>(price) <span class="keyword">AS</span> total</div><div class="line"><span class="keyword">FROM</span> orders</div><div class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span> cust_id</div></pre></td></tr></table></figure>
<ul>
<li>分组计算总和并排序</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">db.orders.aggregate( [</div><div class="line">   &#123;</div><div class="line">     $group: &#123;</div><div class="line">        _id: <span class="string">"$cust_id"</span>,</div><div class="line">        total: &#123; <span class="attr">$sum</span>: <span class="string">"$price"</span> &#125;</div><div class="line">     &#125;</div><div class="line">   &#125;,</div><div class="line">   &#123; <span class="attr">$sort</span>: &#123; <span class="attr">total</span>: <span class="number">1</span> &#125; &#125;</div><div class="line">] )</div></pre></td></tr></table></figure>
<p>映射Sql语句</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> cust_id,</div><div class="line">       <span class="keyword">SUM</span>(price) <span class="keyword">AS</span> total</div><div class="line"><span class="keyword">FROM</span> orders</div><div class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span> cust_id</div><div class="line"><span class="keyword">ORDER</span> <span class="keyword">BY</span> tota</div></pre></td></tr></table></figure>
<ul>
<li>多个字段分组</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">db.orders.aggregate( [</div><div class="line">   &#123;</div><div class="line">     $group: &#123;</div><div class="line">        _id: &#123;</div><div class="line">           cust_id: <span class="string">"$cust_id"</span>,</div><div class="line">           ord_date: &#123;</div><div class="line">               month: &#123; <span class="attr">$month</span>: <span class="string">"$ord_date"</span> &#125;,</div><div class="line">               day: &#123; <span class="attr">$dayOfMonth</span>: <span class="string">"$ord_date"</span> &#125;,</div><div class="line">               year: &#123; <span class="attr">$year</span>: <span class="string">"$ord_date"</span>&#125;</div><div class="line">           &#125;</div><div class="line">        &#125;,</div><div class="line">        total: &#123; <span class="attr">$sum</span>: <span class="string">"$price"</span> &#125;</div><div class="line">     &#125;</div><div class="line">   &#125;</div><div class="line">] )</div></pre></td></tr></table></figure>
<p>映射Sql语句</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> cust_id,</div><div class="line">       ord_date,</div><div class="line">       <span class="keyword">SUM</span>(price) <span class="keyword">AS</span> total</div><div class="line"><span class="keyword">FROM</span> orders</div><div class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span> cust_id,</div><div class="line">         ord_date</div></pre></td></tr></table></figure>
<ul>
<li>条件分组——HAVING</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">db.orders.aggregate( [</div><div class="line">   &#123;</div><div class="line">     $group: &#123;</div><div class="line">        _id: <span class="string">"$cust_id"</span>,</div><div class="line">        count: &#123; <span class="attr">$sum</span>: <span class="number">1</span> &#125;</div><div class="line">     &#125;</div><div class="line">   &#125;,</div><div class="line">   &#123; <span class="attr">$match</span>: &#123; <span class="attr">count</span>: &#123; <span class="attr">$gt</span>: <span class="number">1</span> &#125; &#125; &#125;</div><div class="line">] )</div></pre></td></tr></table></figure>
<p>映射Sql语句</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> cust_id,</div><div class="line">       <span class="keyword">count</span>(*)</div><div class="line"><span class="keyword">FROM</span> orders</div><div class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span> cust_id</div><div class="line"><span class="keyword">HAVING</span> <span class="keyword">count</span>(*) &gt; <span class="number">1</span></div></pre></td></tr></table></figure>
<ul>
<li>复杂条件分组统计</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">db.orders.aggregate( [</div><div class="line">   &#123;</div><div class="line">     $group: &#123;</div><div class="line">        _id: &#123;</div><div class="line">           cust_id: <span class="string">"$cust_id"</span>,</div><div class="line">           ord_date: &#123;</div><div class="line">               month: &#123; <span class="attr">$month</span>: <span class="string">"$ord_date"</span> &#125;,</div><div class="line">               day: &#123; <span class="attr">$dayOfMonth</span>: <span class="string">"$ord_date"</span> &#125;,</div><div class="line">               year: &#123; <span class="attr">$year</span>: <span class="string">"$ord_date"</span>&#125;</div><div class="line">           &#125;</div><div class="line">        &#125;,</div><div class="line">        total: &#123; <span class="attr">$sum</span>: <span class="string">"$price"</span> &#125;</div><div class="line">     &#125;</div><div class="line">   &#125;,</div><div class="line">   &#123; <span class="attr">$match</span>: &#123; <span class="attr">total</span>: &#123; <span class="attr">$gt</span>: <span class="number">250</span> &#125; &#125; &#125;</div><div class="line">] )</div></pre></td></tr></table></figure>
<p>映射Sql语句</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> cust_id,</div><div class="line">       ord_date,</div><div class="line">       <span class="keyword">SUM</span>(price) <span class="keyword">AS</span> total</div><div class="line"><span class="keyword">FROM</span> orders</div><div class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span> cust_id,</div><div class="line">         ord_date</div><div class="line"><span class="keyword">HAVING</span> total &gt; <span class="number">250</span></div></pre></td></tr></table></figure>
<ul>
<li>复杂条件分组统计示例1</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">db.orders.aggregate( [</div><div class="line">   &#123; <span class="attr">$match</span>: &#123; <span class="attr">status</span>: <span class="string">'A'</span> &#125; &#125;,</div><div class="line">   &#123;</div><div class="line">     $group: &#123;</div><div class="line">        _id: <span class="string">"$cust_id"</span>,</div><div class="line">        total: &#123; <span class="attr">$sum</span>: <span class="string">"$price"</span> &#125;</div><div class="line">     &#125;</div><div class="line">   &#125;</div><div class="line">] )</div></pre></td></tr></table></figure>
<p>映射Sql语句</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> cust_id,</div><div class="line">       <span class="keyword">SUM</span>(price) <span class="keyword">as</span> total</div><div class="line"><span class="keyword">FROM</span> orders</div><div class="line"><span class="keyword">WHERE</span> <span class="keyword">status</span> = <span class="string">'A'</span></div><div class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span> cust_id</div></pre></td></tr></table></figure>
<ul>
<li>复杂条件分组统计示例2</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">db.orders.aggregate( [</div><div class="line">   &#123; <span class="attr">$match</span>: &#123; <span class="attr">status</span>: <span class="string">'A'</span> &#125; &#125;,</div><div class="line">   &#123;</div><div class="line">     $group: &#123;</div><div class="line">        _id: <span class="string">"$cust_id"</span>,</div><div class="line">        total: &#123; <span class="attr">$sum</span>: <span class="string">"$price"</span> &#125;</div><div class="line">     &#125;</div><div class="line">   &#125;,</div><div class="line">   &#123; <span class="attr">$match</span>: &#123; <span class="attr">total</span>: &#123; <span class="attr">$gt</span>: <span class="number">250</span> &#125; &#125; &#125;</div><div class="line">] )</div></pre></td></tr></table></figure>
<p>映射Sql语句</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> cust_id,</div><div class="line">       <span class="keyword">SUM</span>(price) <span class="keyword">as</span> total</div><div class="line"><span class="keyword">FROM</span> orders</div><div class="line"><span class="keyword">WHERE</span> <span class="keyword">status</span> = <span class="string">'A'</span></div><div class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span> cust_id</div><div class="line"><span class="keyword">HAVING</span> total &gt; <span class="number">250</span></div></pre></td></tr></table></figure>
<ul>
<li>表关联</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">db.orders.aggregate( [</div><div class="line">   &#123; <span class="attr">$unwind</span>: <span class="string">"$items"</span> &#125;,</div><div class="line">   &#123;</div><div class="line">     $group: &#123;</div><div class="line">        _id: <span class="string">"$cust_id"</span>,</div><div class="line">        qty: &#123; <span class="attr">$sum</span>: <span class="string">"$items.qty"</span> &#125;</div><div class="line">     &#125;</div><div class="line">   &#125;</div><div class="line">] )</div></pre></td></tr></table></figure>
<p>映射Sql语句</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> cust_id,</div><div class="line">       <span class="keyword">SUM</span>(li.qty) <span class="keyword">as</span> qty</div><div class="line"><span class="keyword">FROM</span> orders o,</div><div class="line">     order_lineitem li</div><div class="line"><span class="keyword">WHERE</span> li.order_id = o.id</div><div class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span> cust_id</div></pre></td></tr></table></figure>
<ul>
<li>嵌套查询</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">db.orders.aggregate( [</div><div class="line">   &#123;</div><div class="line">     $group: &#123;</div><div class="line">        _id: &#123;</div><div class="line">           cust_id: <span class="string">"$cust_id"</span>,</div><div class="line">           ord_date: &#123;</div><div class="line">               month: &#123; <span class="attr">$month</span>: <span class="string">"$ord_date"</span> &#125;,</div><div class="line">               day: &#123; <span class="attr">$dayOfMonth</span>: <span class="string">"$ord_date"</span> &#125;,</div><div class="line">               year: &#123; <span class="attr">$year</span>: <span class="string">"$ord_date"</span>&#125;</div><div class="line">           &#125;</div><div class="line">        &#125;</div><div class="line">     &#125;</div><div class="line">   &#125;,</div><div class="line">   &#123;</div><div class="line">     $group: &#123;</div><div class="line">        _id: <span class="literal">null</span>,</div><div class="line">        count: &#123; <span class="attr">$sum</span>: <span class="number">1</span> &#125;</div><div class="line">     &#125;</div><div class="line">   &#125;</div><div class="line">] )</div></pre></td></tr></table></figure>
<p>映射Sql语句</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> <span class="keyword">COUNT</span>(*)</div><div class="line"><span class="keyword">FROM</span> (<span class="keyword">SELECT</span> cust_id,</div><div class="line">             ord_date</div><div class="line">      <span class="keyword">FROM</span> orders</div><div class="line">      <span class="keyword">GROUP</span> <span class="keyword">BY</span> cust_id,</div><div class="line">               ord_date)</div><div class="line">      <span class="keyword">as</span> DerivedTable</div></pre></td></tr></table></figure>
<h2 id="Map-Reduce"><a href="#Map-Reduce" class="headerlink" title="Map-Reduce"></a>Map-Reduce</h2><p>Mongo中聚合查询还有一种叫Map-Reduce，官方文档在<a href="https://docs.mongodb.com/manual/core/map-reduce/" target="_blank" rel="external">这里</a>，在思想上它跟Hadoop一样，从一个单一集合中输入数据，然后将结果输出到一个集合中。通常在使用类似SQL中<code>Group By</code>操作时，Map/Reduce会是一个好的工具。</p>
<p><img src="https://docs.mongodb.com/manual/_images/map-reduce.bakedsvg.svg" alt="Map-Reduce"></p>
<h3 id="接口方法定义"><a href="#接口方法定义" class="headerlink" title="接口方法定义"></a>接口方法定义</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">db.collection.mapReduce(</div><div class="line">    &lt;map&gt;,</div><div class="line">    &lt;reduce&gt;,</div><div class="line">    &#123;</div><div class="line">        out: <span class="xml"><span class="tag">&lt;<span class="name">collection</span>&gt;</span>,</span></div><div class="line">        query: &lt;document&gt;,</div><div class="line">        sort: &lt;document&gt;,</div><div class="line">        limit: &lt;number&gt;,</div><div class="line">        finalize: &lt;function&gt;,</div><div class="line">        scope: &lt;document&gt;,</div><div class="line">        jsMode: &lt;boolean&gt;,</div><div class="line">        verbose: &lt;boolean&gt;,</div><div class="line">        bypassDocumentValidation: &lt;boolean&gt;</div><div class="line">    &#125;</div><div class="line">)</div></pre></td></tr></table></figure>
<h3 id="参数说明"><a href="#参数说明" class="headerlink" title="参数说明"></a>参数说明</h3><ul>
<li><p>mapReduce： 要执行Map/Reduce集合的名字</p>
</li>
<li><p>map: map 函数     (下面会详细介绍）</p>
</li>
<li><p>reduce: reduce函数(下面会详细介绍）</p>
</li>
<li><p>out: 存放结果的集合 (下面会详细介绍）</p>
</li>
<li><p>query: 设置查询条件   &lt;可选&gt;</p>
</li>
<li><p>sort: 按某个键来排序 &lt;可选&gt;</p>
</li>
<li><p>limit: 指明从集合检索文档个数的最大值 &lt;可选&gt;</p>
</li>
<li><p>finalize: 对reduce结果做进一步处理  &lt;可选&gt;</p>
</li>
<li><p>scope: 指明通过map/reduce/finalize可以访问到的变量 &lt;可选&gt;</p>
</li>
<li><p>jsMode: 指明Map/Reduce执行过程中文档保持JSON状态   &lt;可选&gt;</p>
</li>
<li><p>verbose: 提供关于任务执行的统计数据  &lt;可选&gt;</p>
</li>
</ul>
<h2 id="示例说明"><a href="#示例说明" class="headerlink" title="示例说明"></a>示例说明</h2><p>举例说明Map-Reduce的用途，虽然代码比较多，也行用上面的聚合查询，一下子就搞定了，但是这里只是举例。</p>
<p>比如有个订单表，如下所示，我们需要计算每个人的订单总价。</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">&#123;</div><div class="line">     _id: ObjectId("50a8240b927d5d8b5891743c"),</div><div class="line">     cust_id: "abc123",</div><div class="line">     ord_date: new Date("Oct 04, 2012"),</div><div class="line">     status: 'A',</div><div class="line">     price: 25,</div><div class="line">     items: [ &#123; sku: "mmm", qty: 5, price: 2.5 &#125;,</div><div class="line">              &#123; sku: "nnn", qty: 5, price: 2.5 &#125; ]</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>首先定义Map方法，就说我们后面的聚合计算需要哪些字段，由于需要计算每个人的订单总结，那么个人信息和加个肯定是我们需要的。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> mapFunction1 = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">    emit(<span class="keyword">this</span>.cust_id, <span class="keyword">this</span>.price);</div><div class="line">&#125;;</div></pre></td></tr></table></figure>
<p>然后定义reduce方法，计算每个人的订单价格。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> reduceFunction1 = <span class="function"><span class="keyword">function</span>(<span class="params">keyCustId, valuesPrices</span>) </span>&#123;</div><div class="line">    <span class="keyword">return</span> <span class="built_in">Array</span>.sum(valuesPrices);</div><div class="line">&#125;;</div></pre></td></tr></table></figure>
<p>然后存储最后的计算结果。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">db.orders.mapReduce(</div><div class="line">    mapFunction1,</div><div class="line">    reduceFunction1,</div><div class="line">    &#123; <span class="attr">out</span>: <span class="string">"map_reduce_example"</span> &#125;</div><div class="line">)</div></pre></td></tr></table></figure>
<p>这样一个简单的Map-Reduce实例就完成了，结果放在<code>map_reduce_example</code>中。</p>
<p>上面示例比较简单，那么我们来一个复杂一点的例子。</p>
<p>一条订单记录中，有sdk的名称、数量、价格，那么要查询出日期大于<code>01/01/2012</code>，所有订单的总数，以及平均sdk价格。</p>
<p>首先还是定义个map函数。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> mapFunction2 = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> idx = <span class="number">0</span>; idx &lt; <span class="keyword">this</span>.items.length; idx++) &#123;</div><div class="line">        <span class="keyword">var</span> key = <span class="keyword">this</span>.items[idx].sku;</div><div class="line">        <span class="keyword">var</span> value = &#123;</div><div class="line">                        count: <span class="number">1</span>,</div><div class="line">                        qty: <span class="keyword">this</span>.items[idx].qty</div><div class="line">                    &#125;;</div><div class="line">        emit(key, value);</div><div class="line">    &#125;</div><div class="line">&#125;;</div></pre></td></tr></table></figure>
<p>然后算出sku的数量，和总价格。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> reduceFunction2 = <span class="function"><span class="keyword">function</span>(<span class="params">keySKU, countObjVals</span>) </span>&#123;</div><div class="line">    reducedVal = &#123; <span class="attr">count</span>: <span class="number">0</span>, <span class="attr">qty</span>: <span class="number">0</span> &#125;;</div><div class="line"></div><div class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> idx = <span class="number">0</span>; idx &lt; countObjVals.length; idx++) &#123;</div><div class="line">        reducedVal.count += countObjVals[idx].count;</div><div class="line">        reducedVal.qty += countObjVals[idx].qty;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="keyword">return</span> reducedVal;</div><div class="line">&#125;;</div></pre></td></tr></table></figure>
<p>总价格出来后，还要计算出平均价格。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> finalizeFunction2 = <span class="function"><span class="keyword">function</span> (<span class="params">key, reducedVal</span>) </span>&#123;</div><div class="line">    reducedVal.avg = reducedVal.qty / reducedVal.count;</div><div class="line">    <span class="keyword">return</span> reducedVal;</div><div class="line">&#125;;</div></pre></td></tr></table></figure>
<p>还有日期的条件过滤，最后得出完整的map-reduce。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">db.orders.mapReduce(</div><div class="line">    mapFunction2,</div><div class="line">    reduceFunction2,</div><div class="line">    &#123;</div><div class="line">        out: &#123; <span class="attr">merge</span>: <span class="string">"map_reduce_example"</span> &#125;,</div><div class="line">        query: &#123;</div><div class="line">            ord_date:&#123; <span class="attr">$gt</span>: <span class="keyword">new</span> <span class="built_in">Date</span>(<span class="string">'01/01/2012'</span>) &#125;</div><div class="line">        &#125;,</div><div class="line">        finalize: finalizeFunction2</div><div class="line">    &#125;</div><div class="line">)</div></pre></td></tr></table></figure>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>以上就是我对MongoDB的示例总结，本人是一个初学者，也有很多地方不懂，如果有错误的地方，欢迎指出。</p>
<h2 id="相关资料"><a href="#相关资料" class="headerlink" title="相关资料"></a>相关资料</h2><p><a href="http://www.jianshu.com/p/9e1d34eb1d4c" target="_blank" rel="external">浅谈MongoDB数据库</a></p>
<p><a href="https://docs.mongodb.com/manual/tutorial/query-documents/" target="_blank" rel="external">普通查询官方文档</a></p>
<p><a href="https://docs.mongodb.com/manual/reference/sql-comparison/" target="_blank" rel="external">Sql和Mongo隐射表</a></p>
<p><a href="https://docs.mongodb.com/manual/core/aggregation-pipeline/" target="_blank" rel="external">聚合官方文档</a></p>
<p><a href="https://docs.mongodb.com/manual/core/map-reduce/" target="_blank" rel="external">Map-Reduce官方文档</a></p>
<p><a href="https://docs.mongodb.com/manual/reference/method/db.collection.mapReduce/#db.collection.mapReduce" target="_blank" rel="external">Map-Reduce API</a></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechat_pay.jpeg" alt="流水不腐小夏 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpeg" alt="流水不腐小夏 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/MongoDB/" rel="tag"># MongoDB</a>
          
            <a href="/tags/Map-reduce/" rel="tag"># Map reduce</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/10/13/MyMockServer/" rel="next" title="使用MyMockServer模拟数据">
                <i class="fa fa-chevron-left"></i> 使用MyMockServer模拟数据
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/10/25/Android-RePlugin/" rel="prev" title="是时候来一波Android插件化了">
                是时候来一波Android插件化了 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.jpeg"
                alt="流水不腐小夏" />
            
              <p class="site-author-name" itemprop="name">流水不腐小夏</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">55</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/LiushuiXiaoxia" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-GitHub"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://www.jianshu.com/u/58771f980bac" target="_blank" title="简书">
                    
                      <i class="fa fa-fw fa-简书"></i>简书</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://blog.csdn.net/guijiaoba" target="_blank" title="CSDN">
                    
                      <i class="fa fa-fw fa-CSDN"></i>CSDN</a>
                </span>
              
            
          </div>

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#MongoDB查询总结"><span class="nav-number">1.</span> <span class="nav-text">MongoDB查询总结</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#介绍"><span class="nav-number">1.1.</span> <span class="nav-text">介绍</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#普通查询"><span class="nav-number">1.2.</span> <span class="nav-text">普通查询</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#查询举例"><span class="nav-number">1.2.1.</span> <span class="nav-text">查询举例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#聚合查询"><span class="nav-number">1.3.</span> <span class="nav-text">聚合查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Map-Reduce"><span class="nav-number">1.4.</span> <span class="nav-text">Map-Reduce</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#接口方法定义"><span class="nav-number">1.4.1.</span> <span class="nav-text">接口方法定义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#参数说明"><span class="nav-number">1.4.2.</span> <span class="nav-text">参数说明</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#示例说明"><span class="nav-number">1.5.</span> <span class="nav-text">示例说明</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#总结"><span class="nav-number">1.6.</span> <span class="nav-text">总结</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#相关资料"><span class="nav-number">1.7.</span> <span class="nav-text">相关资料</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2015 &mdash; <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">流水不腐小夏</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.3</div>




        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  

  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  

    
      <script id="dsq-count-scr" src="https://mycommons-cn.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'http://mycommons.cn/2017/10/24/MongoDB-Query/';
          this.page.identifier = '2017/10/24/MongoDB-Query/';
          this.page.title = 'MongoDB查询总结';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://mycommons-cn.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  




  
  
  
  <link rel="stylesheet" href="/lib/algolia-instant-search/instantsearch.min.css">

  
  
  <script src="/lib/algolia-instant-search/instantsearch.min.js"></script>
  

  <script src="/js/src/algolia-search.js?v=5.1.3"></script>



  

  

  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
